<template>
  <div class="com-container">
    <div class="com-chart" ref="stock"></div>
  </div>
</template>

<script>
export default {
  created() {
    // 在组件创建完成后，进行回调函数注册
    this.$socket.registerCallBack("stockData", this.getData);
  },
  mounted() {
    // 初始化Charts
    this.initChart();
    // 获取数据
    // this.getData();
    // 告诉服务器，我现在需要数据
    this.$socket.send({
      action: "getData",
      socketType: "stockData",
      chartName: "stock",
      value: "",
    });
    // 适配
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timerId);
    this.$socket.unRegisterCallBack("stockData");
  },
  data() {
    return {
      // 图表的实例对象
      chartInstance: null,
      // 从服务器中获取的所有数据
      allData: null,
      // 当前显示的页数
      currentIndex: 0,
      // 展示多少个数据
      pageSize: 5,
      timerId: null,
      // 圆环坐标
      centerArr: [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"],
      ],
      // 圆环渐变色
      colorArr: [
        ["#4FF778", "#0BA82C"],
        ["#E5DD45", "#E8B11C"],
        ["#E8821C", "#E55445"],
        ["#5052EE", "#AB6EE5"],
        ["#23E5E5", "#2E72BF"],
      ],
    };
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.stock, "chalk");
      const initOption = {
        title: {
          text: "▎库存和销量分析",
          left: 20,
          top: 20,
        },
        tooltip: {
          // 这里为item 可以为内部的数据开启 单独的 tooltip
          trigger: "item",
          backgroundColor: "#222733",
          borderColor: "#666666",
          textStyle: {
            color: "#fff",
          },
        },
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData(res) {
      // 获取地图的散点数据
      // const { data: res } = await this.$http.get("stock");
      this.allData = res;
      console.log("表格数据");
      console.log(res);
      this.updateChart();
      this.startInterval();
    },
    updateChart() {
      // currentIndex: 0,
      let start = this.currentIndex * this.pageSize;
      let end = (this.currentIndex + 1) * this.pageSize;
      let showData = this.allData.slice(start, end);
      let seriesArr = showData.map((item, index) => {
        return {
          type: "pie",
          // radius: [110, 100],
          center: this.centerArr[index],
          hoverAnimation: false, // 关闭鼠标移入饼图时的动画效果
          labelLine: {
            show: false, // 隐藏指示线
          },
          label: {
            position: "center",
            color: this.colorArr[index][0],
          },
          data: [
            // 销量
            {
              name: item.name + "\n\n" + item.sales,
              value: item.sales,
              itemStyle: {
                // 创建线性渐变的颜色 从下往上
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  // 0%
                  { offset: 0, color: this.colorArr[index][0] },
                  // 100%
                  { offset: 1, color: this.colorArr[index][1] },
                ]),
              },
              // 内部的提示框 c数值 d百分比
              tooltip: {
                formatter: `${item.name} <br/>销量：{c} <br/>占比：{d}%`,
              },
            },
            // 库存
            {
              name: item.name + "\n\n" + item.sales,
              value: item.stock,
              itemStyle: {
                color: "#333843",
              },
              // 内部的提示框
              tooltip: {
                formatter: `${item.name} <br/>库存：{c} <br>占比：{d}%`,
              },
            },
          ],
        };
      });

      let dataOption = {
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    // 浏览器大小发生变化会调用此方法
    screenAdapter() {
      const titleFontSize = (this.$refs.stock.offsetWidth / 100) * 3.6;
      const innerRadius = titleFontSize * 2;
      const outerRadius = innerRadius * 1.125;
      let adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            type: "pie",
            radius: [outerRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outerRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outerRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outerRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outerRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex * this.pageSize >= this.allData.length) {
          this.currentIndex = 0;
        }
        this.updateChart();
      }, 4000);
    },
  },
};
</script>

<style></style>
